package com.top.horizon.th1804app.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.net.protocol.model.RecordFile;
import com.top.horizon.th1804app.R;
import com.top.horizon.th1804app.bean.PlayListBean;
import com.net.protocol.NetController;
import com.top.horizon.th1804app.utils.ToastUitl;

import java.util.ArrayList;
import java.util.List;

public class PlayBackAdapter extends RecyclerView.Adapter<PlayBackAdapter.PlayBackViewHolder> {
    //被选中，高亮显示
    private int selected = 0;
    private List<RecordFile> mPlayBackList;
    private Context mContext;
    private PlayBackViewHolder playBackViewHolder;
    private String[] deleteFiles = new String[1];
    private onPlayAdapterListener mListener;

    public PlayBackAdapter(List<RecordFile> list, Context context) {
        this.mPlayBackList = list;
        this.mContext = context;

    }

    public void setListener(onPlayAdapterListener listener){
        mListener = listener;
    }

    public void setSelected(int selected){
        if (selected < 0 || selected >= mPlayBackList.size()){
            return;
        }
        this.selected = selected;
        notifyDataSetChanged();
    }

    @Override
    public PlayBackViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflate = View.inflate(mContext, R.layout.play_back_item, null);
        playBackViewHolder = new PlayBackViewHolder(inflate);
        return playBackViewHolder;
    }

    @Override
    public void onBindViewHolder(final PlayBackViewHolder holder, final int position) {
        final RecordFile playListBean = mPlayBackList.get(position);
        if (playListBean != null) {
            holder.tvFileName.setText(playListBean.name);
            holder.tvIsPlaying.setText(playListBean.getPlayDesc());
            holder.tvTime.setText(playListBean.startTime.split(" ")[1]+"-"+
                    playListBean.endTime.split(" ")[1]);
        }
        //此处想做模拟点击播放改变条目背景颜色
        if (position == selected) {
            holder.itemView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.colorPrimaryDark));
        } else {
            holder.itemView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.white));
        }
        //移除按钮点击事件，删除选中文件
        holder.btnDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null){
                    mListener.deleteFile(position,playListBean);
                }
            }
        });
        //条目点击
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null){
                    mListener.clickItem(position,playListBean);
                }
            }
        });
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                if (mListener != null){
                    mListener.longClickItem(position,playListBean);
                }
                return true;
            }
        });
    }

    @Override
    public int getItemCount() {
        return mPlayBackList.size();
    }

    public class PlayBackViewHolder extends RecyclerView.ViewHolder {
        TextView tvFileName;
        TextView tvIsPlaying;
        TextView btnDelete;
        TextView tvTime;

        public PlayBackViewHolder(View itemView) {
            super(itemView);
            tvFileName = itemView.findViewById(R.id.tv_file_name);
            tvIsPlaying = itemView.findViewById(R.id.tv_is_playing);
            btnDelete = itemView.findViewById(R.id.btn_delete);
            tvTime = itemView.findViewById(R.id.tv_time);

        }
    }

    public interface onPlayAdapterListener{
        void deleteFile(int position,RecordFile file);
        void clickItem(int position,RecordFile file);
        void longClickItem(int position,RecordFile file);
    }
}
